package com.situ.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mvc.common.ServerResponse;
import com.situ.mvc.entity.Department;
import com.situ.mvc.entity.User;
import com.situ.mvc.service.IUserService;
import com.situ.mvc.service.impl.UserServiceImpl;

@Controller
@RequestMapping(value = "/user")
public class UserController {
	
	private IUserService userService = new UserServiceImpl();

	@RequestMapping(value = "/getLoginPage")
	public String getLoginPage(){
		return "login";
	}
	
	@RequestMapping(value = "/login")
	@ResponseBody
	public ServerResponse login(String name,String password){
		System.out.println("name:  "+name);
		System.out.println("password:  "+password);
		ServerResponse response = userService.login(name,password);
		return response;
	}
	
	@RequestMapping(value = "/index")
	public String index(){
		return "index";
	}
	
	@RequestMapping(value = "/getUserPageList")
	public String getPageList(){
		return "user_page_list";
	}
	
	@RequestMapping(value = "/getList")
	@ResponseBody
	public ServerResponse getList(Integer page,Integer limit){
		ServerResponse response = userService.findList(page,limit);
		return response;
	}
	
	@RequestMapping(value = "/getAddUserPage")
	public String getAddUserPage(){
		return "user_add";
	}
	
	@RequestMapping(value = "/getDepartmentSelect")
	@ResponseBody
	public ServerResponse getDepartmentSelect(){
		ServerResponse response = userService.getSelect();
		return response;
	}
	
	@RequestMapping(value = "/addUser")
	@ResponseBody
	public ServerResponse addUser(User user){
		ServerResponse response = userService.addUser(user);
		return response;
	}
	
	@RequestMapping(value ="/deleteAll")
	@ResponseBody
	public ServerResponse deleteAll(String ids){
		ServerResponse response = userService.deleteAll(ids);
		return response;
	}
	
	@RequestMapping(value = "/deleteById")
	@ResponseBody
	public ServerResponse deleteById(Integer id){
		ServerResponse response = userService.deleteById(id);
		return response;
	}
	
	@RequestMapping(value = "/toUpdate")
	public String toUpdate(Integer id,Model model){
		User user = userService.toUpdate(id);
		System.out.println("user:"+user);
		model.addAttribute("user", user);
		return "user_update";
	}
	
	@RequestMapping(value = "/getDepartmentPageList")
	public String getDepartmentPageList(){
		return "department_list";
	}
	
	@RequestMapping(value = "/getDepartmentList")
	@ResponseBody
	public ServerResponse getDepartmentList(){
		ServerResponse response = userService.getDepartmentList();
		return response;
	}
	
	@RequestMapping(value = "/deleteByDepartmentId")
	@ResponseBody
	public ServerResponse deleteByDepartmentId(Integer id){
		ServerResponse response = userService.deleteByDepartmentId(id);
		return response;
	}
	
	@RequestMapping(value = "/getUpdateDepartmentPage")
	public String getUpdateDepartmentPage(Integer id,Model model){
		Department department = userService.getUpdateDepartmentPage(id);
		model.addAttribute("department", department);
		return "department_update";
	}
	
	@RequestMapping(value = "/userUpdate")
	@ResponseBody
	public ServerResponse userUpdate(User user){
		ServerResponse response = userService.userUpdate(user);
		return response;
	}
	
	
}
